import { Injectable } from '@nestjs/common';
import { CreateSettleDto } from './dto/create-settle.dto';
import { UpdateSettleDto } from './dto/update-settle.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Settle } from './entities/settle.entity';
import { ILike, Repository } from 'typeorm';
import { loadPageQueryResult } from 'src/utils/utils';

@Injectable()
export class SettleService {
  constructor(@InjectRepository(Settle) private readonly settleRepository: Repository<Settle>) { }
  create(createSettleDto: CreateSettleDto) {
    return 'This action adds a new settle';
  }
  //list
  async findAll(pageNum: number, pageSize: number, playWithName?: string) {
    let res = await this.settleRepository.findAndCount({
      where: {
        playWithName: playWithName ? ILike(`%${playWithName}%`) : undefined
      },
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
      order: { id: 'DESC' }
    })

    return {
      code: 1,
      data: loadPageQueryResult<[Settle[], number]>(res, pageNum, pageSize)
    };
  }

  findOne(id: number) {
    return `This action returns a #${id} settle`;
  }

  update(id: number, updateSettleDto: UpdateSettleDto) {
    return `This action updates a #${id} settle`;
  }

  remove(id: number) {
    return `This action removes a #${id} settle`;
  }
  async statement(playWithId: string) {
    await this.settleRepository.update({ playWithId }, { settleAmount: 0, settleStatus: '1' })
    return {
      code: 1,
      message: "结算成功"
    }
  }
}
